import 'package:efood_multivendor/controller/splash_controller.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

class DateConverter {
  static String formatDate(DateTime dateTime) {
    return DateFormat('yyyy-MM-dd hh:mm:ss a').format(dateTime);
  }

  static String dateToTimeOnly(DateTime dateTime) {
    return DateFormat(_timeFormatter()).format(dateTime);
  }

  static String convertUtcToEat(String utcTimeString) {
    final utcTime = DateTime.parse(utcTimeString);
    final eatOffset = Duration(hours: 3);
    final eatTime = utcTime.add(eatOffset);
    final dateFormat = DateFormat('yyyy-MM-dd hh:mm:ss');
    final eatTimeString = dateFormat.format(eatTime);
    return eatTimeString;
  }

  static String dateToDateAndTime(DateTime dateTime) {
    return DateFormat('yyyy-MM-dd HH:mm').format(dateTime);
  }

  static String dateTimeStringToDateTime(String dateTime) {
    return DateFormat('dd MMM yyyy  ${_timeFormatter()}')
        .format(DateFormat('yyyy-MM-dd HH:mm:ss').parse(dateTime));
  }

  static String dateTimeStringToDateOnly(String dateTime) {
    return DateFormat('dd MMM yyyy')
        .format(DateFormat('yyyy-MM-dd HH:mm:ss').parse(dateTime));
  }

  static DateTime dateTimeStringToDate(String dateTime) {
    return DateFormat('yyyy-MM-dd HH:mm:ss').parse(dateTime);
  }

  static DateTime isoStringToLocalDate(String dateTime) {
    return DateFormat('yyyy-MM-ddTHH:mm:ss.SSS').parse(dateTime);
  }

  static String isoStringToDateTimeString(String dateTime) {
    return DateFormat('dd MMM yyyy  ${_timeFormatter()}')
        .format(isoStringToLocalDate(dateTime));
  }

  static String isoStringToLocalDateOnly(String dateTime) {
    return DateFormat('dd MMM yyyy').format(isoStringToLocalDate(dateTime));
  }

  static String stringToLocalDateOnly(String dateTime) {
    return DateFormat('dd MMM yyyy')
        .format(DateFormat('yyyy-MM-dd').parse(dateTime));
  }

  static String localDateToIsoString(DateTime dateTime) {
    return DateFormat('yyyy-MM-ddTHH:mm:ss.SSS').format(dateTime);
  }

  static String convertTimeToTime(String time) {
    return DateFormat(_timeFormatter()).format(DateFormat('HH:mm').parse(time));
  }

  static DateTime convertStringTimeToDate(String time) {
    return DateFormat('HH:mm').parse(time);
  }

  static bool isAvailable(String? start, String? end,
      {DateTime? time, bool isoTime = false}) {
    DateTime _currentTime;
    if (time != null) {
      _currentTime = time;
    } else {
      _currentTime = Get.find<SplashController>().currentTime;
    }
    DateTime _start = start != null
        ? isoTime
            ? isoStringToLocalDate(start)
            : DateFormat('HH:mm').parse(start)
        : DateTime(_currentTime.year);
    DateTime _end = end != null
        ? isoTime
            ? isoStringToLocalDate(end)
            : DateFormat('HH:mm').parse(end)
        : DateTime(
            _currentTime.year, _currentTime.month, _currentTime.day, 23, 59);
    DateTime _startTime = DateTime(_currentTime.year, _currentTime.month,
        _currentTime.day, _start.hour, _start.minute, _start.second);
    DateTime _endTime = DateTime(_currentTime.year, _currentTime.month,
        _currentTime.day, _end.hour, _end.minute, _end.second);
    if (_endTime.isBefore(_startTime)) {
      if (_currentTime.isBefore(_startTime) &&
          _currentTime.isBefore(_endTime)) {
        _startTime = _startTime.add(Duration(days: -1));
      } else {
        _endTime = _endTime.add(Duration(days: 1));
      }
    }
    return _currentTime.isAfter(_startTime) && _currentTime.isBefore(_endTime);
  }

  static String _timeFormatter() {
    return Get.find<SplashController>().configModel?.timeformat == '24'
        ? 'HH:mm'
        : 'hh:mm a';
  }

  static DateTime getCurrentTimeInGmtPlus3() {
    DateTime utcNow = DateTime.now().toUtc();
    DateTime gmtPlus3 = utcNow.add(Duration(hours: 3));
    return gmtPlus3;
  }
}
